package com.umbrella.worker.service.impl;

import java.util.Calendar;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.umbrella.worker.dao.WOperateMapper;
import com.umbrella.worker.dao.WRoleOperateMapper;
import com.umbrella.worker.dto.OperateDO;
import com.umbrella.worker.entity.WOperate;
import com.umbrella.worker.entity.WOperateExample;
import com.umbrella.worker.entity.WRoleOperateExample;
import com.umbrella.worker.query.OperateQuery;
import com.umbrella.worker.result.ResultDO;
import com.umbrella.worker.result.ResultSupport;
import com.umbrella.worker.service.IOperateService;
import com.umbrella.worker.util.BeanUtilsExtends;
import com.umbrella.worker.util.StringUtil;
@Service("operateService")
public class OperateServiceImpl extends BaseServiceImpl implements IOperateService {
	
	private static Logger logger = Logger.getLogger(OperateServiceImpl.class);
	@Autowired
	private WOperateMapper operateMapper;
	@Autowired
	private WRoleOperateMapper roleOperateMapper;

	@Override
	public ResultDO create(OperateDO operateDO) {
		
		WOperate operate = new WOperate();
		
		ResultSupport result = BeanUtilsExtends.copy(operate, operateDO);
		
		if(!result.isSuccess()) {
			return result;
		}
		
		int recordNum = -1;
		
		operate.setDatalevel(1);
		operate.setStatus(1);
		operate.setCreateTime(Calendar.getInstance().getTime());
		operate.setModifiTime(Calendar.getInstance().getTime());
		
		try {
			recordNum = operateMapper.insertSelective(operate);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			logger.error("[obj:contact][opt:create][msg:" + e.getMessage()
			+ "]");
			return result;
		}
		
		if(recordNum == 1) {
			result.setModel(ResultDO.FIRST_MODEL_KEY, operate.getId());
		} else {
			result.setSuccess(false);
		}
		return result;
	}

	@Override
	public ResultDO modifi(OperateDO operateDO) {
		
		WOperate operate = new WOperate();

		ResultSupport result = BeanUtilsExtends.copy(operate, operateDO);
		// 拷贝失败
		if (!result.isSuccess()) {
			return result;
		}
		operate.setModifiTime(Calendar.getInstance().getTime());
		int recordNum = -1;
		try {
			recordNum = operateMapper.updateByPrimaryKeySelective(operate);
		} catch (Exception e) {
			result.setSuccess(false);
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			logger.error("[obj:contact][opt:modifi][msg:" + e.getMessage()
					+ "]");
			return result;
		}
		if (recordNum < 1) {
			result.setSuccess(false);
		}

		return result;
	}

	@Override
	public ResultDO remove(int id) {
		ResultSupport result = new ResultSupport();
		
		WOperate operate = new WOperate();
		
		if(StringUtil.isGreatOne(id)) {
			operate.setId(id);
		} else {
		    result.setSuccess(false);
		    return result;
		}
		operate.setDatalevel(-1);
		int recordNum = -1;
		try {
			recordNum = operateMapper.updateByPrimaryKeySelective(operate);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:contact][opt:remove][msg:"+e.getMessage()+"]");
	        return result;
		}
		
	
		WRoleOperateExample example = new WRoleOperateExample();
		example.createCriteria().andWOperateIdEqualTo(id);
	
		recordNum = -1;
		try {
			recordNum = roleOperateMapper.deleteByExample(example);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:contact][opt:remove][msg:"+e.getMessage()+"]");
	        return result;
		}
		if (recordNum != 1) {
			result.setSuccess(false);
		}
		return result;
	}

	@Override
	public ResultDO get(int id) {
		
		ResultSupport result = new ResultSupport();
		
		WOperate operate = null;
		if(!StringUtil.isGreatOne(id)) {
			 result.setSuccess(false);
			 return result;
		} 
		
		try {
			operate = operateMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:supplier][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		OperateDO operateDO = getOperateDO(operate);
		if(operateDO != null) {
			result.setModel(ResultSupport.FIRST_MODEL_KEY, operateDO);
		} else {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
			return result;
		}
		
		return result;
	}

	@Override
	public ResultDO list(OperateQuery operateQuery) {
		
		ResultSupport result = new ResultSupport();
		
		WOperateExample example = new WOperateExample();
		WOperateExample.Criteria c = example.createCriteria();
		
		if(StringUtil.isGreatOne(operateQuery.getwOType())) {
			c.andWOTypeEqualTo(operateQuery.getwOType());
		}

		c.andDatalevelEqualTo(1);
	
		List<WOperate> list = null;
		
		try {
			list = operateMapper.selectByExample(example);
		} catch (Exception e) {
			result.setSuccess(false);
	        result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
	        result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG);
	        logger.error("[obj:supplier][opt:get][msg:"+e.getMessage()+"]");
	        return result;
		}
		
		List<OperateDO> operatelist = getOperateDOList(list);
			
		result.setModel(ResultSupport.FIRST_MODEL_KEY, operatelist);
		
		return result;
	}


}
